/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesodatosembebido;

/**
 *
 * @author $Danny Riofrio Jimenez
 */
import java.sql.*;

/**
 * HelloWorld_JDBCTutorial_Main : JDBC Tutorial that creates a simple database,
 * stores in a single row and retrieves the data to then be displayed on
 * standard out. The database is cleaned up to reduce clutter for this example.
 * Also note that the main throws a SQLException. Normally this code would be
 * declared in a separate method which can throw an exception or contain another
 * try catch block, and then be caught in a try catch block where used, but for
 * example sake we throw it from the main.
*
 */
public class HelloWorld_JDBCTutorial_main {

    public static void main(String[] args) throws SQLException {


        Connection Conn = DriverManager.getConnection("jdbc:raima:rdm://local");

        try {

            Statement Stmt = Conn.createStatement();
            try {
                try { //Since the database is created here, it cannot be created twice
                    Stmt.execute("DROP DATABASE HelloWorldDb");
                } catch (SQLException exception) {
                }

                Stmt.execute("CREATE DATABASE HelloWorldDb");
                Stmt.execute("CREATE TABLE hello_table (myChar char(31))");
                Conn.commit();

                PreparedStatement PrepStmt =
                        Conn.prepareStatement("INSERT INTO hello_table (myChar) VALUES (?)");
                try { //Sets parameter value to a string         
                    PrepStmt.setString(1, "Hello World!");
                    PrepStmt.execute();
                    Conn.commit();

                    ResultSet RS = Stmt.executeQuery("SELECT * FROM hello_table");
                    try {
                        while (RS.next() != false) {
                            System.out.println(RS.getString(1));
                        }
                    } finally {
                        RS.close();
                    }
                } finally { //Cleans up by dropping database in this case
                    Stmt.execute("DROP DATABASE HelloWorldDb");
                    PrepStmt.close();
                }
            } finally {
                Stmt.close();
            }

        } catch (SQLException exception) {
            System.err.println("SQLException: " + exception.toString());

        } finally {
            Conn.close();
        }
    }
}
